package thired.class08_tree;

public class Class07_isSearch {

    public static class Node {
        public int value;
        public Node left;
        public Node right;

        public Node(int v) {
            value = v;
        }
    }

    public static class Info{
        public boolean isS;
        public int max;
        public int min;

        public Info(boolean isS, int max, int min) {
            this.isS = isS;
            this.max = max;
            this.min = min;
        }
    }

    public static boolean isS(Node root){
        if(root == null){
            return true;
        }
      return  process(root).isS;
    }

    public static Info process(Node root){
        if(root == null){
            return null;
        }
        Info left = process(root.left);
        Info right = process(root.right);
        int max = root.value;
        if(left != null){
            max = Math.max(max, left.max);
        }
        if(right != null){
            max = Math.max(max, right.max);
        }
        int min = root.value;
        if(left != null){
            min = Math.min(min, left.min);
        }
        if(right != null){
            min = Math.min(min, right.min);
        }
        boolean isS = true;
        int x = root.value;
        if (left != null && !left.isS) {
            isS = false;
        }
        if (right != null && !right.isS) {
            isS = false;
        }
        if (left != null && left.max >= x) {
            isS = false;
        }
        if (right != null && right.min <= x) {
            isS = false;
        }
        return new Info(isS, max, min);

    }




    public static boolean isS2(Node head) {
        if (head == null) {
            return true;
        }
        return process1(head).isS;
    }

    public static Info process1(Node x) {
        if (x == null) {
            return null;
        }
        Info leftInfo = process(x.left);
        Info rightInfo = process(x.right);
        int max = x.value;
        if (leftInfo != null) {
            max = Math.max(max, leftInfo.max);
        }
        if (rightInfo != null) {
            max = Math.max(max, rightInfo.max);
        }
        int min = x.value;
        if (leftInfo != null) {
            min = Math.min(min, leftInfo.min);
        }
        if (rightInfo != null) {
            min = Math.min(min, rightInfo.min);
        }
        boolean isS = true;
        if (leftInfo != null && !leftInfo.isS) {
            isS = false;
        }
        if (rightInfo != null && !rightInfo.isS) {
            isS = false;
        }
        if (leftInfo != null && leftInfo.max >= x.value) {
            isS = false;
        }
        if (rightInfo != null && rightInfo.min <= x.value) {
            isS = false;
        }
        return new Info(isS, max, min);
    }

    // for test
    public static Node generateRandomBST(int maxLevel, int maxValue) {
        return generate(1, maxLevel, maxValue);
    }

    // for test
    public static Node generate(int level, int maxLevel, int maxValue) {
        if (level > maxLevel || Math.random() < 0.5) {
            return null;
        }
        Node head = new Node((int) (Math.random() * maxValue));
        head.left = generate(level + 1, maxLevel, maxValue);
        head.right = generate(level + 1, maxLevel, maxValue);
        return head;
    }

    public static void main(String[] args) {
        int maxLevel = 4;
        int maxValue = 100;
        int testTimes = 1000000;
        for (int i = 0; i < testTimes; i++) {
            Node head = generateRandomBST(maxLevel, maxValue);
            if (isS(head) != isS2(head)) {
                System.out.println("Oops!");
            }
        }
        System.out.println("finish!");
    }

}
